<?php
/*
	Copyright 2006, 2007, 2008, 2009, 2010 Bastiaan Grutters
    
    This file is part of Ages of Strife website.

    Ages of Strife website is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    Ages of Strife website is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with Ages of Strife website.  If not, see <http://www.gnu.org/licenses/>.
 */
include( '../global/initialize.php' );

$query = "SELECT count(*) FROM village";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$villages = $row[ 'count(*)' ];

$query = "SELECT count(*) FROM ruler";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$rulers = $row[ 'count(*)' ];

$query = "SELECT count(*) FROM faction";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );
$factions = $row[ 'count(*)' ];

$query2 = "SELECT SUM(population) FROM village";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$query3 = "SELECT SUM(scout+infantry+cavalry+artillery) FROM army";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop =  $row2[ 'SUM(population)' ] + $row3[ 'SUM(scout+infantry+cavalry+artillery)' ];
$query3 = "SELECT SUM(recruits) FROM ruler";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop += $row3[ 'SUM(recruits)' ];
$query3 = "SELECT SUM(population) FROM colony";
$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
$world_pop += $row3[ 'SUM(colony)' ];

$query = "SELECT * FROM game";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$row = mysql_fetch_array( $result, MYSQL_ASSOC );

$query2 = "SELECT count(*) FROM round WHERE name = '" . $row[ 'round_name' ] . "'";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );

$update = false;
if( $row2[ 'count(*)' ] > 0 ) {
	$query = "UPDATE round SET turns = " . $row[ 'turn' ] . ", rulers = $rulers, villages = $villages, " .
			"factions = $factions, population = $world_pop, speed = " . $row[ 'speed' ] . " WHERE name = '" . $row[ 'round_name' ] . "'";
	//print( "query: $query<br />" );
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$_SESSION[ 'store_stats_status' ] = "Round settings updated.";
	$update = true;
}
else {
	$query = "INSERT INTO round ( name, turns, rulers, villages, factions, population, speed ) " .
			"VALUES ( '" . $row[ 'round_name' ] . "', " . $row[ 'turn' ] . ", $rulers, $villages, " .
			"$factions, $world_pop, " . $row[ 'speed' ] . " )";
	//print( "query: $query<br />" );
	mysql_query( $query ) or die( "Query failed : " . mysql_error() );
	$_SESSION[ 'store_stats_status' ] = "Round settings stored.";
}
//print( "test<br />" );

$query2 = "SELECT round_id FROM round WHERE name = '" . $row[ 'round_name' ] . "'";
$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
$round_id = $row2[ 'round_id' ];

$query = "SELECT * FROM ruler ORDER by economy_points DESC LIMIT 10";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
while( $i < $num ) {
	$query2 = "SELECT end_username, username " .
			"FROM users " .
			"JOIN ruler ON ruler.user_id = users.user_id " .
			"WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$end_username = $row2[ 'end_username' ];
	$username = $row2[ 'username' ];

	$query2 = "SELECT count(*) FROM village WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	if( $end_username == 1 ) {
		$name = mysql_result( $result, $i, "name" ) . ' (' . $username . ')';
	}
	else {
		$name = mysql_result( $result, $i, "name" );
	}

	$query3 = "SELECT id FROM hall_of_fame WHERE round_id = $round_id AND name = '" . $name . "'";
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
	
	if( isset( $row3[ 'id' ] ) ) {
		$query3 = "UPDATE hall_of_fame SET score = " . mysql_result( $result, $i, "score" ) . ", battle_score = " . mysql_result( $result, $i, "experience" ) . "" .
				", economy = " . mysql_result( $result, $i, "economy_points" ) . ", war = " . mysql_result( $result, $i, "warrior_points" ) . ", " .
				"villages = " . $row2[ 'count(*)' ] . " WHERE id = " . $row3[ 'id' ];
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	else {
		$query3 = "INSERT INTO hall_of_fame ( name, score, battle_score, economy, war, villages, round_id, leader ) " .
			"VALUES ( '" . $name . "', " . mysql_result( $result, $i, "score" ) . ", " . mysql_result( $result, $i, "experience" ) . "" .
			", " . mysql_result( $result, $i, "economy_points" ) . ", " . mysql_result( $result, $i, "warrior_points" ) . "" .
			", " . $row2[ 'count(*)' ] . ", $round_id, '' )";
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	$i ++;
}

$query = "SELECT * FROM ruler ORDER by warrior_points DESC LIMIT 10";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
while( $i < $num ) {
	$query2 = "SELECT end_username, username " .
			"FROM users " .
			"JOIN ruler ON ruler.user_id = users.user_id " .
			"WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$end_username = $row2[ 'end_username' ];
	$username = $row2[ 'username' ];

	$query2 = "SELECT count(*) FROM village WHERE ruler_id = " . mysql_result( $result, $i, "ruler_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	
	if( $end_username == 1 ) {
		$name = mysql_result( $result, $i, "name" ) . ' (' . $username . ')';
	}
	else {
		$name = mysql_result( $result, $i, "name" );
	}

	$query3 = "SELECT id FROM hall_of_fame WHERE round_id = $round_id AND name = '" . $name . "'";
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
	
	if( isset( $row3[ 'id' ] ) ) {
		$query3 = "UPDATE hall_of_fame SET score = " . mysql_result( $result, $i, "score" ) . ", battle_score = " . mysql_result( $result, $i, "experience" ) . "" .
				", economy = " . mysql_result( $result, $i, "economy_points" ) . ", war = " . mysql_result( $result, $i, "warrior_points" ) . ", " .
				"villages = " . $row2[ 'count(*)' ] . " WHERE id = " . $row3[ 'id' ];
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	else {
		$query3 = "INSERT INTO hall_of_fame ( name, score, battle_score, economy, war, villages, round_id, leader ) " .
			"VALUES ( '" . $name . "', " . mysql_result( $result, $i, "score" ) . ", " . mysql_result( $result, $i, "experience" ) . "" .
			", " . mysql_result( $result, $i, "economy_points" ) . ", " . mysql_result( $result, $i, "warrior_points" ) . "" .
			", " . $row2[ 'count(*)' ] . ", $round_id, '' )";
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	$i ++;
}

$query = "SELECT * FROM faction ORDER by score DESC LIMIT 10";
$result = mysql_query( $query ) or die( "Query failed : " . mysql_error() );
$num = mysql_numrows( $result );
$i = 0;
while( $i < $num ) {
	$query2 = "SELECT count(*) FROM village JOIN faction_member ON faction_member.ruler_id = village.ruler_id WHERE faction_member.faction_id = " . mysql_result( $result, $i, "faction_id" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$villages = $row2[ 'count(*)' ];
	
	$query2 = "SELECT count(*) FROM faction_member WHERE faction_id = " . mysql_result( $result, $i, "faction_id" ) . " AND requested = 0 AND invited = 0";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$members = $row2[ 'count(*)' ];

	$query2 = "SELECT SUM(experience) FROM ruler JOIN faction_member ON faction_member.ruler_id = ruler.ruler_id WHERE faction_id = " . mysql_result( $result, $i, "faction_id" ) . " AND requested = 0 AND invited = 0";
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$experience = $row2[ 'SUM(experience)' ];

	$query2 = "SELECT name FROM ruler WHERE ruler_id = " . mysql_result( $result, $i, "leader" );
	$result2 = mysql_query( $query2 ) or die( "Query failed : " . mysql_error() );
	$row2 = mysql_fetch_array( $result2, MYSQL_ASSOC );
	$leader = $row2[ 'name' ];

	$query3 = "SELECT id FROM hall_of_fame WHERE round_id = $round_id AND name = '" . mysql_result( $result, $i, "name" ) . "'";
	$result3 = mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	$row3 = mysql_fetch_array( $result3, MYSQL_ASSOC );
	
	if( isset( $row3[ 'id' ] ) ) {
		$query3 = "UPDATE hall_of_fame SET score = " . mysql_result( $result, $i, "score" ) . ", battle_score = $experience" .  
				", economy = " . mysql_result( $result, $i, "economy_points" ) . ", war = " . mysql_result( $result, $i, "war_points" ) . ", " .
				"villages = " . $villages . ", leader = '$leader', members = $members WHERE id = " . $row3[ 'id' ];
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	else {
		$query3 = "INSERT INTO hall_of_fame ( name, score, battle_score, economy, war, villages, members, round_id, leader, faction ) " .
			"VALUES ( '" . mysql_result( $result, $i, "name" ) . "', " . mysql_result( $result, $i, "score" ) . ", battle_score = $experience" .
			", " . mysql_result( $result, $i, "economy_points" ) . ", " . mysql_result( $result, $i, "war_points" ) . "" .
			", " . $villages . ", $members, $round_id, '$leader', 1 )";
		mysql_query( $query3 ) or die( "Query failed : " . mysql_error() );
	}
	$i ++;
}

header( "Location: admin.php" );
?>
